﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Algobox.JuicyLib.SQL.LiquidView;

namespace Algobox.JuicyLib.MarketData
{
    public class MarketRecorder
    {
        public MarketRecorder()
        {
            LiquidViewLinq = new LiquidViewTest01DataContext();
            PopulateCache();
        }

        public MarketRecorder(string connection)
        {
            LiquidViewLinq = new LiquidViewTest01DataContext(connection);
            PopulateCache();
        }
        
        Algobox.JuicyLib.SQL.LiquidView.LiquidViewTest01DataContext LiquidViewLinq;

        public string ConnectionString { get { return LiquidViewLinq.Connection.ConnectionString; } }

        Dictionary<string, long> instrumentIdsCache = new Dictionary<string, long>();
        public Dictionary<string, long> InstrumentIdsCache
        {
            get { return instrumentIdsCache; }
        }

        private void PopulateCache()
        {
            string key;

            List<GetViewInstrumentsStockResult> istruments = new List<GetViewInstrumentsStockResult>(LiquidViewLinq.GetViewInstrumentsStock());

            foreach (GetViewInstrumentsStockResult istrument in istruments)
            {
                key = istrument.ISIN + istrument.Currency + istrument.MarketMIC;

                instrumentIdsCache[key] = istrument.InstrumentId;
            }
        }

        public void InsertBBO(DateTime updateTime, long instrumentId, decimal bidPrice, long bidVolume, bool bidHasChanged, decimal askPrice, long askVolume, bool askHasChanged, string phase)
        {
            LiquidViewLinq.ImportDataLevel1BBOs(updateTime, instrumentId, bidPrice, bidVolume, bidHasChanged, askPrice, askVolume, askHasChanged, phase);
        }

        public void InsertLast(DateTime updateTime, long instrumentId, decimal lastPrice, long lastVolume, decimal bidPrice, long bidVolume, decimal askPrice, long askVolume, string phase)
        {
            LiquidViewLinq.ImportDataLevel1Trades(updateTime, instrumentId, lastPrice, lastVolume, bidPrice, bidVolume, askPrice, askVolume, phase);
        }
    }
}
